#include<iostream>
#include<algorithm>
#include<unordered_map>

using namespace std;
const int N = 2e6 + 10;
int f[N];
int idx;
int n;
unordered_map<int, int>mp;
struct Query
{
	int x, y, e;
}query[N];
int find(int x)
{
	if (x != f[x]) {
		f[x] = find(f[x]);
	}
	return f[x];
}
int get(int x)
{
	if (mp.count(x) == 0) {
		mp[x] = ++n;
	}
	return mp[x];
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		n = 0;
		int m = 0;
		mp.clear();
		cin >> m;
		for (int i = 0; i < m; i++) {
			int a, b, e;
			cin >> a >> b >> e;
			query[i] = { get(a),get(b),e };
		}
		for (int i = 1; i <= n; i++) {
			f[i] = i;
		}
		for (int i = 0; i < m; i++) {
			if (query[i].e == 1) {
				int pa = find(query[i].x);
				int pb = find(query[i].y);
				f[pa] = pb;
			}
		}
		bool has_confict = 0;
		for (int i = 0; i < m; i++) {
			if (query[i].e == 0) {
				int pa = find(query[i].x);
				int pb = find(query[i].y);
				if (pa == pb) {
					has_confict = 1;
					break;
				}
			}
		}
		if (has_confict) {
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl;
		}
	}
	return 0;
}